{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q trino"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "CATALOG_URL = \"http://lakekeeper:8181/catalog\"\n",
    "TRINO_URI = \"https://trino-proxy:443\"\n",
    "KEYCLOAK_TOKEN_ENDPOINT = \"http://keycloak:8080/realms/iceberg/protocol/openid-connect/token\"\n",
    "WAREHOUSE = \"demo\"\n",
    "\n",
    "CLIENT_ID = \"trino\"\n",
    "CLIENT_SECRET = \"AK48QgaKsqdEpP9PomRJw7l2T7qWGHdZ\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Connect to Trino\n",
    "In this example, Trino is configured to also use oauth. We are now logging into trino as a human user `peter`.\n",
    "The following cell should print a link. Open this link in a new tab and login as:\n",
    "* User: `peter`\n",
    "* Password: `iceberg`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from trino.dbapi import connect\n",
    "from trino.auth import OAuth2Authentication\n",
    "from redirect_handler import REDIRECT_HANDLER\n",
    "import urllib3\n",
    "\n",
    "urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n",
    "\n",
    "conn = connect(\n",
    "    host=TRINO_URI,\n",
    "    auth=OAuth2Authentication(REDIRECT_HANDLER),\n",
    "    http_scheme=\"https\",\n",
    "    verify=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cur = conn.cursor()\n",
    "print(\"\\nResult of 'SELECT 1: \", cur.execute(\"Select 1\").fetchall())\n",
    "print(\"Result of 'SELECT current_user': \", cur.execute(\"SELECT current_user\").fetchall())\n",
    "# The current_user has now the unique ID from our IdP!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating the Trino Catalog\n",
    "Make sure to follow the instructions in the `01-Bootstrap.ipynb` and `02-Create-Warehouse.ipynb` notebooks first!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cur = conn.cursor()\n",
    "cur.execute(\n",
    "    f\"\"\"\n",
    "    CREATE CATALOG lakekeeper USING iceberg\n",
    "    WITH (\n",
    "        \"iceberg.catalog.type\" = 'rest',\n",
    "        \"iceberg.rest-catalog.uri\" = '{CATALOG_URL}',\n",
    "        \"iceberg.rest-catalog.warehouse\" = '{WAREHOUSE}',\n",
    "        \"iceberg.rest-catalog.security\" = 'OAUTH2',\n",
    "        \"iceberg.rest-catalog.oauth2.credential\" = '{CLIENT_ID}:{CLIENT_SECRET}',\n",
    "        \"iceberg.rest-catalog.vended-credentials-enabled\" = 'true',\n",
    "        \"iceberg.rest-catalog.oauth2.scope\" = 'lakekeeper',\n",
    "        \"iceberg.rest-catalog.oauth2.server-uri\" = '{KEYCLOAK_TOKEN_ENDPOINT}',\n",
    "        \"s3.region\"= 'dummy',\n",
    "        \"s3.path-style-access\" = 'true',\n",
    "        \"s3.endpoint\" = 'http://minio:9000',\n",
    "        \"fs.native-s3.enabled\" = 'true'\n",
    "    )\n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
